package com.googlecode.shapeline.web.context;

import javax.servlet.*;
import org.springframework.context.*;
import org.springframework.web.context.*;
import com.googlecode.shapeline.web.context.support.*;
import com.googlecode.shapeline.startup.*;

public class ContextLoaderListener implements ServletContextListener {
	private final static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(ContextLoaderListener.class.getName());
	private ContextLoader contextLoader;
	private Bootstrap bootstrap;
	
	public void contextInitialized(ServletContextEvent event) {
		log.info("-=-=-=-=-=- APPLICATION STARTING... =-=-=-=-=-=-=-");

		log.info("spring context initializing...");
		contextLoader = new ContextLoader();
		ApplicationContext applicationContext = contextLoader.initWebApplicationContext(event.getServletContext());
		//log.debug("application context: " + applicationContext);
		ApplicationContextUtils.getInstance().setApplicationContext(applicationContext);
		log.info("spring context initialized");
		
		//bootstrap = (Bootstrap)applicationContext.getBean("bootstrap");
		//bootstrap.start();
	}
	
	public void contextDestroyed(ServletContextEvent event) {
		if (this.contextLoader != null) {
			this.contextLoader.closeWebApplicationContext(event.getServletContext());
		}
		
		bootstrap.stop();
		log.info("APPLICATION STOPPED");
	}
}